<?php
/**
 * Created by IntelliJ IDEA.
 * User: wanglu
 * Date: 2017/7/19
 * Time: 14:13
 */

/**
 * Created by wanglu on 2017/7/19.
 */
class Lang extends AdminBase
{

    function __construct()
    {
        parent::__construct();
        $this->load->service('user_service');
        $this->load->service('project_service');
        $this->load->service('lang_service');
    }


    /**
     * 项目语言
     */
    public function lang()
    {
        $pid = $this->input->get("pid");
        $langs = $this->lang_service->get_langs_by_pid($pid);
        $project_user_relation = $this->project_service->get_project_user_relation($pid, $this->loginUser['id']);
        $this->showView("tpl_project_lang", array('langs' => $langs, 'project_user_relation' => $project_user_relation));
    }

    /**
     * 选择项目语言
     */
    public function get_lang_choices()
    {
        $pid = $this->input->get("pid");
        $langs = $this->lang_service->get_langs_by_pid($pid);
        $this->showView("tpl_project_lang_choice", array("langs" => $langs));
    }

    /**
     * 删除语言
     */
    public function delete() {
        $id = $this->input->post("id");
        if ($this->lang_service->delete_lang($id)) {
            die(json_encode(array("code" => 200)));
        } else {
            die(json_encode(array("code" => 201, "msg" => '操作失败')));
        }
    }

    //public function add


    public function add() {

        $pid = $this->input->post("pid");
        $name = $this->input->post("name");
        $shorthand = $this->input->post("shorthand");
        $remark = $this->input->post("remark");

        if ($this->lang_service->add_lang($pid, $name, $shorthand, $remark)) {
            die(json_encode(array("code" => 200)));
        } else {
            die(json_encode(array("code" => 201, "msg" => '操作失败')));
        }

    }

    public function update() {
        $pid = $this->input->post("pid");
        $lang_id = $this->input->post("lang_id");
        $name = $this->input->post("name");
        $shorthand = $this->input->post("shorthand");
        $remark = $this->input->post("remark");

        if ($this->lang_service->update_lang($pid, $lang_id, $name, $shorthand, $remark)) {
            die(json_encode(array("code" => 200)));
        } else {
            die(json_encode(array("code" => 201, "msg" => '操作失败')));
        }
    }


    public function get() {
        $lang_id = $this->input->post("lang_id");
        $lang = $this->lang_service->get_lang_by_id($lang_id);
        echo json_encode($lang);
    }

    public function get_user_permission() {
        $uid  = $this->input->post("uid");
        $pid  = $this->input->post("pid");
        $lang_persimssions = $this->lang_service->get_lang_permission($pid, $uid);

        echo json_encode($lang_persimssions);
    }
}